package com.xs.ahoi.hcxs.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;

/**
 * 系统亮度设置
 */
public class LightUtil {

    // 获取当前系统亮度值
    public static int getLight(Activity activity) {
        int brightValue = 0;
        ContentResolver contentResolver = activity.getContentResolver();
        try {
            brightValue = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
        }
        return brightValue;
    }

    // 改变屏幕亮度
    public static int setLight(Activity activity, int brightValue) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.screenBrightness = (brightValue < 0 ? -1.0f : brightValue / 255f);
        activity.getWindow().setAttributes(lp);
        return brightValue;
    }

    // 设置为系统亮度
    public static int setSystemLight(Activity activity) {
        return setLight(activity, getLight(activity));
    }

    // 设置屏幕常亮
    public static void keepScreenAlwaysLight(Activity activity) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    // 取消屏幕常亮
    public static void disableScreenAlwaysLight(Activity activity) {
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
}
